//
// Created by Administrator on 2024/1/31.
//
#include <vector>
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;


class Solution
{
public:
    vector<int> findKDistantIndices(vector<int>& nums, int key, int k)
    {
        set<int> collector;
        for (int i = 0; i < nums.size(); ++i)
        {
            if (nums[i] == key)
            {
                collector.insert(i);
                for (int j = i - 1; j >= i - k and j>=0; j--)
                {
                    collector.insert(j);
                }
                for (int j = i + 1; j <= i + k and j<=nums.size()-1; j++)
                {
                    collector.insert(j);
                }
            }
        }
        vector<int> ans;
        ans.reserve(collector.size());
        for (auto x : collector)
        {
            ans.push_back(x);
        }
        sort(ans.begin(), ans.end());
        return ans;
    }
    vector<int> findKDistantIndices2(vector<int>& nums, int key, int k)
    {
        vector<int> res;
        int n = nums.size();
        // 遍历数对
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if (nums[j] == key && abs(i - j) <= k) {
                    res.push_back(i);
                    break;   // 提前结束以防止重复添加
                }
            }
        }
        return res;
    }
};

int main()
{
    vector<int> nums{3, 4, 9, 1, 3, 9, 5};
    Solution solution;
    auto ans = solution.findKDistantIndices2(nums, 9, 1);
    for (auto x : ans) cout << x << endl;
    return 0;
}
